#!/usr/bin/env python

import os
import wx
import time
import datetime
import ConfigParser
import logging.config
import resource.string.app as rApp

class Meta(object):
    '''Meta Class
    
    @author Julot <andy.julot@gmail.com>
    @since Friday, January 13, 2012. 04:07 PM'''
    
    def __init__(self, **kw):
        self.name = rApp.name
        self.description = rApp.description
        self.long_description = rApp.longDescription
        self.version = rApp.version
        self.histories = rApp.histories
        self.trademarks = rApp.trademarks
        self.copyright = rApp.copyright
        self.company_name = rApp.company
        self.comments = rApp.blessing
        self.script = 'main.py'
        self.icon = os.path.join(os.getcwdu(), "resource", "icon", 'app.ico')
        self.icon_resources = [(1, self.icon)]
        self.dest_base = '..\\%s' % self.name
        self.author = rApp.author
        self.author_email = rApp.authorEmail
        self.maintainer = rApp.author
        self.maintainer_email = rApp.authorEmail
        self.logging_config = '%s.logging.conf' % self.name
        self.long_version = rApp.humanVersion
        self.manifest = '''
        <assembly
          xmlns="urn:schemas-microsoft-com:asm.v1"
          manifestVersion="1.0"
        >
          <assemblyIdentity
            version="%s"
            processorArchitecture="x86"
            name="%s"
            type="win32"
          />
          <description>%s</description>
          <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
            <security>
              <requestedPrivileges>
                <requestedExecutionLevel
                  level="asInvoker"
                  uiAccess="false"
                />
              </requestedPrivileges>
            </security>
          </trustInfo>
          <dependency>
            <dependentAssembly>
              <assemblyIdentity
                type="win32"
                name="Microsoft.VC90.CRT"
                version="9.0.21022.8"
                processorArchitecture="x86"
                publicKeyToken="1fc8b3b9a1e18e3b"
              />
            </dependentAssembly>
          </dependency>
          <dependency>
            <dependentAssembly>
              <assemblyIdentity
                type="win32"
                name="Microsoft.Windows.Common-Controls"
                version="6.0.0.0"
                processorArchitecture="x86"
                publicKeyToken="6595b64144ccf1df"
                language="*"
              />
            </dependentAssembly>
          </dependency>
        </assembly>
        ''' % (self.version, self.name, self.name)
        self.other_resources = [(24, 1, self.manifest)]
        
        self.__dict__.update(kw)

class Config(wx.FileConfig):
    '''Config class
    
    @author Julot <andy.julot@gmail.com>
    @since Friday, January 13, 2012. 04:07 PM'''
    
    def __init__(self):
        meta = Meta()
        s = '%s.conf' % meta.name
        filename = os.path.join(os.getcwdu(), s)
        wx.FileConfig.__init__(self, meta.name, meta.company_name, filename)
    
class App(wx.App):
    '''App class
    
    @author Julot <andy.julot@gmail.com>
    @since Sunday, January 15, 2012. 07:22 PM'''
    
    def __init__(self, *args, **kwargs):
        wx.App.__init__(self, *args, **kwargs)
        self.initLogging()
    
    def initLogging(self):
        meta = Meta()
        if not os.path.lexists('log'): os.mkdir('log')
        if not os.path.lexists(meta.logging_config): self._createLoggingConfig()
        logging.config.fileConfig(meta.logging_config)
        
    def _createLoggingConfig(self):
        config = ConfigParser.RawConfigParser()
        
        config.add_section('loggers')
        config.set('loggers', 'keys', 'root')
        
        config.add_section('logger_root')
        config.set('logger_root', 'handlers', 'debug,warning')
        config.set('logger_root', 'level', 'NOTSET')
        
        config.add_section('formatters')
        config.set('formatters', 'keys', 'simple,complex')
        
        config.add_section('formatter_simple')
        s = '[%(asctime)s] [%(levelname)s] %(message)s'
        config.set('formatter_simple', 'format', s)
        
        config.add_section('formatter_complex')
        s = '[%(asctime)s] [%(levelname)s] [%(module)s : '
        s += '%(lineno)d] %(message)s'
        config.set('formatter_complex', 'format', s)
        
        config.add_section('handlers')
        config.set('handlers', 'keys', 'debug,warning')
        
        config.add_section('handler_debug')
        config.set('handler_debug', 'class', 'handlers.RotatingFileHandler')
        config.set('handler_debug', 'maxBytes', '1024')
        config.set('handler_debug', 'backupCount', '30')
        config.set('handler_debug', 'formatter', 'complex')
        config.set('handler_debug', 'level', 'DEBUG')
        s = "(os.path.join('log', 'debug.log'),)"
        config.set('handler_debug', 'args', s)
        
        config.add_section('handler_warning')
        config.set('handler_warning', 'class', 'handlers.RotatingFileHandler')
        config.set('handler_warning', 'maxBytes', '1024')
        config.set('handler_warning', 'backupCount', '30')
        config.set('handler_warning', 'formatter', 'simple')
        config.set('handler_warning', 'level', 'WARNING')
        s = "(os.path.join('log', 'warning.log'),)"
        config.set('handler_warning', 'args', s)
        
        with open(Meta().logging_config, 'wb') as configFile:
            config.write(configFile)
    